/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.poll.factor.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.poll.factor.dto.PollFactorInfoExcelDTO;
import com.hopes.bizz.poll.factor.entity.PollFactorInfoEntity;
import com.hopes.bizz.poll.factor.service.PollFactorInfoService;
import com.hopes.bizz.poll.factor.vo.PollFactorInfoExcelVO;
import com.hopes.boss.admin.api.entity.SysDictItem;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.excel.annotation.RequestExcel;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import com.hopes.boss.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 因子信息
 *
 * @author daijie
 * @date 2023-05-19 09:37:47
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/factor")
@Tag(description = "factor", name = "因子信息管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class PollFactorInfoController {

    private final PollFactorInfoService pollFactorInfoService;

    /**
     * 分页查询
     *
     * @param page           分页对象
     * @param pollFactorInfo 因子信息
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('poll_factor_view')")
    public R getpollFactorInfoPage(Page page, PollFactorInfoEntity pollFactorInfo) {
        LambdaQueryWrapper<PollFactorInfoEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StrUtil.isNotBlank(pollFactorInfo.getName()), PollFactorInfoEntity::getName, pollFactorInfo.getName());
        wrapper.eq(ObjectUtil.isNotNull(pollFactorInfo.getUseFlag()), PollFactorInfoEntity::getUseFlag, pollFactorInfo.getUseFlag());
        wrapper.orderBy(true, false, PollFactorInfoEntity::getSortNum);
        return R.ok(pollFactorInfoService.page(page, wrapper));
    }

    /**
     * 列表查询
     * @param pollFactorInfo   查询对象
     * @return                 结果
     */
    @Operation(summary = "列表查询", description = "列表查询")
    @GetMapping("/list")
    @PreAuthorize("@pms.hasPermission('poll_factor_view')")
    public R getPollFactorInfoList(PollFactorInfoEntity pollFactorInfo) {
        LambdaQueryWrapper<PollFactorInfoEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StrUtil.isNotBlank(pollFactorInfo.getName()), PollFactorInfoEntity::getName, pollFactorInfo.getName());
        wrapper.eq(ObjectUtil.isNotNull(pollFactorInfo.getUseFlag()), PollFactorInfoEntity::getUseFlag, pollFactorInfo.getUseFlag());
        wrapper.eq(ObjectUtil.isNotNull(pollFactorInfo.getFactorType()), PollFactorInfoEntity::getFactorType, pollFactorInfo.getFactorType());
        wrapper.orderBy(true, false, PollFactorInfoEntity::getSortNum);
        return R.ok(pollFactorInfoService.list(wrapper));
    }

    /**
     * 通过id查询因子信息
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('poll_factor_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(pollFactorInfoService.getById(id));
    }

    /**
     * 新增因子信息
     *
     * @param pollFactorInfo 因子信息
     * @return R
     */
    @Operation(summary = "新增因子信息", description = "新增因子信息")
    @SysLog("新增因子信息")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('poll_factor_add')")
    public R save(@RequestBody PollFactorInfoEntity pollFactorInfo) {
        return R.ok(pollFactorInfoService.save(pollFactorInfo));
    }

    /**
     * 修改因子信息
     *
     * @param pollFactorInfo 因子信息
     * @return R
     */
    @Operation(summary = "修改因子信息", description = "修改因子信息")
    @SysLog("修改因子信息")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('poll_factor_edit')")
    public R updateById(@RequestBody PollFactorInfoEntity pollFactorInfo) {
        return R.ok(pollFactorInfoService.updateById(pollFactorInfo));
    }

    /**
     * 通过id删除因子信息
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除因子信息", description = "通过id删除因子信息")
    @SysLog("通过id删除因子信息")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('poll_factor_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(pollFactorInfoService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * excel模板
     *
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/template")
    @PreAuthorize("@pms.hasPermission('poll_factor_export')")
    public List<PollFactorInfoExcelDTO> template() {
        return Collections.singletonList(new PollFactorInfoExcelDTO());
    }

    /**
     * 导出excel 表格
     *
     * @param pollFactorInfoExcelDto 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('poll_factor_export')")
    public List<PollFactorInfoExcelVO> export(PollFactorInfoExcelDTO pollFactorInfoExcelDto) {
        return pollFactorInfoService.exportExcel(pollFactorInfoService.list());
    }

    /**
     * 导入excel 表格
     *
     * @param pollFactorInfoExcelDtoList 查询条件
     * @param bindingResult              错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('poll_factor_export')")
    public R importExcel(@RequestExcel List<PollFactorInfoExcelDTO> pollFactorInfoExcelDtoList, BindingResult bindingResult) {
        return pollFactorInfoService.importExcel(pollFactorInfoExcelDtoList, bindingResult);
    }

    /**
     * 查询因子信息集合
     *
     */
    @GetMapping("/queryFactor2Dict")
    public R queryFactor2Dict() {
        List<SysDictItem> sysDictItems = pollFactorInfoService.labelValueList();
        return R.ok(sysDictItems);
    }
}
